       PROGRAM NOTABS
C *** LAST REVISED ON 17-SEP-1987 07:27:38.05
C *** SOURCE FILE: [DL.GRAPHICS]NOTABS.FOR
C
C      CREATED:  DGL AUG 1987
C
C      THIS PROGRAM PROCESSES A FORTRAN SOURCE CODE FILE CONTAINING TABS
C      AND TRAILING COMMENTS.  TABS ARE REPLACED WITH SPACES WHILE
C      TRAILING COMMENTS (SEPARATED BY AN EXPLANATION POINT) ARE
C      CONVERTED INTO COMMENT LINES PRECEEDING THE LINES THEY TRAIL.
C
C      THIS PROGRAM IS FORTRAN 77 COMPATIBLE WITH THE EXCEPTIONS:
C              1. A VAX DEPENDENT ROUTINE IS USED TO GET COMMAND LINE.
C
       CHARACTER*80 NAME
       CHARACTER*130 INLINE,OUT,OUT1
       LOGICAL ICUT
C
C      MACHINE DEPENDENT METHOD OF GETING THE FILE NAME FROM THE
C      RUN COMMAND LINE
C
       IERR=LIB$GET_FOREIGN(NAME,,IFLAG)
C
C      IF FILE NAME WAS NOT PRESENT ON ARGUMENT LINE THEN PROMPT FOR
C      FILE NAME.   NOTE: $ CAUSES CURSOR TO BE LEFT AND END OF LINE.
C
       IF (LENREAL(NAME).LT.1) THEN
               WRITE (*,1)
1              FORMAT(' ENTER FILE NAME TO CONVERT: ',$)
               READ (*,2) NAME
2              FORMAT(A80)
       ENDIF
C
C      OPEN INPUT AND OUTPUT FILES
C
       OPEN (UNIT=1,FILE=NAME,STATUS='OLD',FORM='FORMATTED')
       OPEN (UNIT=2,FILE=NAME,STATUS='NEW',FORM='FORMATTED')
C
C      READ INPUT LINE
C
10     CONTINUE
       READ (1,5,END=99) INLINE
5      FORMAT(A130)
C
C      INITIALIZE OUTPUT LINES TO SPACES
C
       DO 15 I=1,130
               OUT(I:I)=CHAR(32)
               OUT1(I:I)=CHAR(32)
15     CONTINUE
C
C      INITIALIZE CHARACTER POINTERS IN OUTPUT LINE (ILOC) AND INPUT
C      LINE (ICNT)
C
       ILOC=1
       ICNT=1
       LMAX=LENREAL(INLINE)
C
C      CHECK EACH CHARACTER IN INPUT LINE FOR:
C              (1) A TAB
C              (2) TRAILING COMMENT SEPERATOR
C              (3) END OF STRING CHARACTER
C
20     CONTINUE
       ICUT=.TRUE.
       IC=ICHAR(INLINE(ICNT:ICNT))
C
C      IF FIRST CHARACTER OF LINE WAS A C, WE WON'T NEED TO
C      CUT OFF TRAILING COMMENTS
C
       IF (IC.EQ.67.OR.IC.EQ.99) ICUT=.FALSE.
C
C      SEE IF CHARACTER IS A TAB AND REPLACE TAB BY SPACES
C
       IF (IC.EQ.9) THEN
               N=((ILOC+8)/8)*8-ILOC
               DO 25 I=1,N
                       OUT(ILOC:ILOC)=CHAR(32)
                       ILOC=ILOC+1
25             CONTINUE
C
       ELSE IF (IC.EQ.33.AND.ICUT) THEN
C
C      HANDLE TRAILING COMMENTS (SEPARATED BY EXPLANATION POINT)
C      BY BREAKING LINE AND OUTPUTING TRAILING COMMENT AS A LINE
C      PRECEEDED BY C.
C
               OUT1(1:1)=CHAR(67)
               ICN=2
               L=LMAX-ICNT+1
               DO 55 I=1,L
                       IC=ICHAR(INLINE(ICNT:ICNT))
                       ICNT=ICNT+1
C
C      REPLACE TRAILING COMMENT TABS BY A SINGLE SPACE
C
                       IF (IC.EQ.9) THEN
                               OUT1(ICN:ICN)=CHAR(32)
                       ELSE IF (IC.EQ.0.OR.IC.EQ.13) THEN
                               GOTO 56
                       ELSE
                               OUT1(ICN:ICN)=CHAR(IC)
                       ENDIF
                       ICN=ICN+1
55             CONTINUE
C
C      OUTPUT COMMENT LINE BEFORE PREVIOUS LINE
C
56             CALL OUTPUT(OUT1,ICN,2)
C
C      OUTPUT CURRENT LINE
C
               CALL OUTPUT(OUT,ILOC,2)
               GOTO 10
       ELSE IF (IC.EQ.0.OR.IC.EQ.13) THEN
C
C      END OF LINE ENCOUNTERED
C
               GOTO 30
       ELSE
C
C      COPY NORMAL CHARACTER
C
               OUT(ILOC:ILOC)=CHAR(IC)
               ILOC=ILOC+1
       ENDIF
C
C      INCREMENT INPUT COUNTER LINE
C
       ICNT=ICNT+1
       IF (ICNT.LE.LMAX) GOTO 20
C
C      OUTPUT NORMAL LINE
C
30     CALL OUTPUT(OUT,ILOC,2)
C
C      READ NEXT LINE
C
       GOTO 10
99     STOP
       END
C
C
       INTEGER FUNCTION LENREAL(LINE)
C
C      COMPUTES THE "REAL" LENGTH OF A LINE AS THE NUMBER OF
C      CHARACTERS WHICH END IN ALL BLANKS.  SEARCHS FROM END OF
C      LINE TO FIND FIRST NON-CONTROL CHARACTER.
C
       CHARACTER*(*) LINE
C
       LENG=LEN(LINE)
       DO 100 I=1,LENG
               J=LENG-I+1
               IC=ICHAR(LINE(J:J))
               IF (IC.GT.32) GOTO 200
100    CONTINUE
       J=0
200    LENREAL=J
       RETURN
       END
C
C
       SUBROUTINE OUTPUT(B,N,LU)
       CHARACTER*(*) B
C
C      COMPUTE LENGTH OF OUTPUT STRING (REMOVE TRAILING SPACES)
C
       J=LENREAL(B)
C
C      DON'T OUTPUT BLANK LINES
C
       IF (J.GT.1) THEN
               WRITE(LU,77) B(1:J)
77             FORMAT(A)
       ELSE IF (J.EQ.1) THEN
               IF (ICHAR(B(1:1)).NE.32.AND.ICHAR(B(1:1)).NE.0)
     $                 WRITE(LU,77) B(1:1)
       ENDIF
70     FORMAT(X,A130)
       RETURN
       END
